from games.models import UserRecord
from rest_framework.authentication import BaseAuthentication

from rest_framework.exceptions import AuthenticationFailed

class LuffyAuth(BaseAuthentication):

    def authenticate(self, request):
        """
        用户请求进行认证
        :param request:
        :return:
        """
        # http://wwwww.../?token=adfasdfasdf
        token = request.query_params.get('token')
        obj = UserRecord.objects.filter(token=token).first()
        if not obj:
            raise AuthenticationFailed({'code':1001,'error':'认证失败'})

        return (obj.token,obj)